package com.ruoyi.video.controller;

import javax.annotation.Resource;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.video.dto.InitTaskDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.video.service.IUploadTaskService;
import com.ruoyi.common.core.web.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * videoController
 *
 * @author Larry
 * @date 2024-03-22
 */
@Slf4j
@RestController
public class UploadTaskController extends BaseController
{

    @Resource
    IUploadTaskService uploadTaskService;
    @Resource
    private RemoteFileService remoteFileService;

    /**
     * 山传文件到minio
     */
     @PostMapping("/upload")
    public R<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
         log.info(String.valueOf(file.getSize()));
         if(file.isEmpty()){
             return R.fail("文件不能为空");
         }
         R<SysFile> fileR = remoteFileService.upload(file);
         System.out.println(fileR.getMsg());
         return R.ok(fileR.getMsg());
    }
    @PostMapping("/init")
    public R<String> init(@RequestBody InitTaskDTO initTaskDTO){
        System.out.println(initTaskDTO.toString());
       return uploadTaskService.initTask(initTaskDTO);
    }
    @GetMapping("/check")
    public R<String> check(String md5){
       return uploadTaskService.check(md5);
    }
    @GetMapping("/slice")
    public R<Integer> getSlice(String md5){
         return uploadTaskService.getSlice(md5);
    }
    @PostMapping("/uploadSlice")
    public R<String> uploadSlice(@RequestParam("md5")String md5,@RequestParam("now") Integer now,@RequestParam("file") MultipartFile file) throws Exception {
        if(file.isEmpty()){
            return R.fail("文件不能为空");
        }
        uploadTaskService.uploadSlice(md5,now,file);
        return R.ok("上传成功!");
    }
}
